package com.seed.uaa.web.role;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;
import com.seed.application.service.BaseService;
import com.seed.core.pojo.SetDTO;
import com.seed.uaa.pojo.RoleAction;
import com.seed.uaa.util.PermiUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.seed.uaa.web.role.table.RoleTableDef.ROLE;
import static com.seed.uaa.web.user.table.UserTableDef.USER;

@Service
public class RoleService extends BaseService<RoleMapper, Role> {

    public RoleService() {
        super(Role.class);
    }

    public String getActions(String id) {
        QueryWrapper queryWrapper = query()
                .select(ROLE.ID, ROLE.ACTIONS)
                .where(ROLE.ID.eq(id));

        Role role = getMapper().selectOneByQuery(queryWrapper);
        if(role == null) throw new RuntimeException("数据不存在");

        return role.getActions();
    }

    public boolean setActions(SetDTO dto) {
        String id = dto.getId();
        Role role = one(id);
        if(role == null) throw new RuntimeException("数据不存在");

        String value = dto.getValue();
        role.setActions(value);
        return getMapper().update(role) > 0;
    }

    public List<String> getDatas(String id, String actionPath) {
        QueryWrapper queryWrapper = query()
                .select(USER.ID, USER.DATAS)
                .where(USER.ID.eq(id));

        Role role = getMapper().selectOneByQuery(queryWrapper);
        if(role == null) throw new RuntimeException("数据不存在");

        String datas = role.getDatas();
        if(StrUtil.isBlank(datas)) return Collections.emptyList();

        return PermiUtil.extract(datas, actionPath);
    }

    public boolean setDatas(String id, String actionPath, List<String> operates) {
        QueryWrapper queryWrapper = query()
                .select(ROLE.ID, ROLE.DATAS, ROLE.VERSION)
                .where(ROLE.ID.eq(id));

        Role role = getMapper().selectOneByQuery(queryWrapper);
        if(role == null) throw new RuntimeException("数据不存在");

        role.setDatas(PermiUtil.edit(role.getDatas(), actionPath, operates));

        return getMapper().update(role) > 0;
    }

    public List<String> getFields(String id, String actionPath) {
        QueryWrapper queryWrapper = query()
                .select(ROLE.ID, ROLE.FIELDS)
                .where(ROLE.ID.eq(id));

        Role role = getMapper().selectOneByQuery(queryWrapper);
        if(role == null) throw new RuntimeException("数据不存在");

        String fields = role.getFields();
        if(StrUtil.isBlank(fields)) return Collections.emptyList();

        return PermiUtil.extract(fields, actionPath);
    }

    public boolean setFields(String id, String actionPath, Map<String, String> params) {
        QueryWrapper queryWrapper = query()
                .select(ROLE.ID, ROLE.FIELDS, ROLE.VERSION)
                .where(ROLE.ID.eq(id));

        Role role = getMapper().selectOneByQuery(queryWrapper);
        if(role == null) throw new RuntimeException("数据不存在");

        role.setFields(PermiUtil.edit(role.getFields(), actionPath, params));
        return getMapper().update(role) > 0;
    }

    private Role one(String id) {
        QueryWrapper queryWrapper = query()
                .select(ROLE.ID, ROLE.VERSION)
                .where(ROLE.ID.eq(id));
        return getMapper().selectOneByQuery(queryWrapper);
    }

    private RoleAction getRoleAction(String id) {
        Role role = QueryChain.of(Role.class)
                .select(ROLE.ID, ROLE.NAME)
                .where(ROLE.ID.eq(id))
                .one();
        if(role == null) throw new RuntimeException("数据不存在");

        RoleAction ret = new RoleAction(id, role.getName());
        String actions = role.getActions();
        if(StrUtil.isNotBlank(actions)) {
            ret.setActions(List.of(actions.split(",")));
        }

        return ret;
    }
}
